from mg_app_framework import MesCode
from tornado_request_mapping import request_mapping

from common_bind_service.common.base.base_handler import BaseHandler
from common_bind_service.models.param.code_param import CheckGenerateCoilNoParam, CheckGenerateCellSnParam, \
    CheckBindAndUnbindCodeParam
from common_bind_service.pkg import auth
from common_bind_service.pkg.log.log_decorator import log_decorator
from common_bind_service.service.code_generation_service import CodeGenerationService


@request_mapping('/generate_code')
class CodeGenerationHandler(BaseHandler):

    def __init__(self, *args, **kwargs) -> None:
        super().__init__(*args, **kwargs)
        self.code_generate_service = CodeGenerationService()

    @request_mapping('/generate_coil_no', 'post', 'F031上位机上报极卷原料码，向MES获取分切后的卷料在制品码')
    @log_decorator()
    async def generate_coil_no(self):
        """
        上位机上报极卷原料码，向MES获取分切后的卷料在制品码。
        MES检查上位机上报的极卷原料码是否在系统存在，通过后按上位机上传的第一个极卷原料码、生成指定数量的卷料批次码并返回给上位机
        """

        data = self.data
        params = self.check_params(CheckGenerateCoilNoParam, **data)
        status, err = auth.check_upperpc_callid(params.upperpc_code, params.call_id)
        if not status:
            return self.send_response_data(MesCode.fail, None, err)
        response = await self.code_generate_service.generate_coil_no(params)
        return self.send_response_data(response.code, response.data, response.msg)

    @request_mapping('/generate_cell_sn', 'post', 'F030电芯码/蓝胶码生成通用接口')
    @log_decorator()
    async def generate_cell_sn(self):
        """上位机上传关联批次信息，请求产出的在制品条码（蓝胶码/电芯码），MES根据规则生成对应的唯一条码。"""
        data = self.data
        params = self.check_params(CheckGenerateCellSnParam, **data)
        status, err = auth.check_upperpc_callid(params.upperpc_code, params.call_id)
        if not status:
            return self.send_response_data(MesCode.fail, None, err)
        response = await self.code_generate_service.generate_cell_sn(params)
        return self.send_response_data(response.code, response.data, response.msg)

    @request_mapping('/wip_inout_bind', 'post', 'F28:与F30生成的code做绑定')
    @log_decorator()
    async def wip_inout_bind(self):
        """上位机上传进站码和出站码的绑定关系，MES校验进站码和出站码有没有和其它条码重复绑定，校验通过后建立绑定关系											"""
        data = self.data
        params = self.check_params(CheckBindAndUnbindCodeParam, **data)
        status, err = auth.check_upperpc_callid(params.upperpc_code, params.call_id)
        if not status:
            return self.send_response_data(MesCode.fail, None, err)
        response = await self.code_generate_service.generate_bind_code(params)
        return self.send_response_data(response.code, response.data, response.msg)

    @request_mapping('/wip_inout_unbind', 'post', 'F29:与F30生成的code做解绑')
    @log_decorator()
    async def wip_inout_unbind(self):
        """上位机上传进站码和出站码的解绑请求，MES校验进站码和出站码的绑定关系，校验通过后解除绑定关系											"""
        data = self.data
        params = self.check_params(CheckBindAndUnbindCodeParam, **data)
        status, err = auth.check_upperpc_callid(params.upperpc_code, params.call_id)
        if not status:
            return self.send_response_data(MesCode.fail, None, err)
        response = await self.code_generate_service.unbind_code(params)
        return self.send_response_data(response.code, response.data, response.msg)
